package com.smh.a02PaiXuDemo2;

import java.util.Random;

public class ChaRuPaiXu {
    public static void main(String[] args) {

        int[] arr = {12, 34, 51, 21, 23, 12, 32, 43, 12, 3, 234, 2};
        charu(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }

        int[] arr2 = new int[10000];
        Random random = new Random();
        for (int i = 0; i < arr2.length; i++) {
            arr2[i]=random.nextInt();
        }
        Long start = System.currentTimeMillis();
        charu(arr2);
        Long end = System.currentTimeMillis();
        System.out.println(end-start);

    }

    public static int[] charu(int[] arr) {
        int startIndex = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > arr[i]) {
                startIndex = i;
            }
        }

        for (int i = startIndex; i < arr.length - 1; i++) {
//            if (arr[i]>arr[i+1]) {
//                for (int j = i+1; j >0; j--) {
//                    int flag=0;
//                    if (arr[j]<arr[j-1]){
//                        int temp = arr[j];
//                        arr[j] = arr[j-1];
//                        arr[j-1] = temp;
//                        flag=1;
//                    }
//                    if (flag==0){
//                        break;
//                    }
//                }
//            }
            int j = i;
            while (j >= 0 && arr[j+1] < arr[j]) {
                int temp = arr[j+1];
                arr[j+1] = arr[j];
                arr[j] = temp;
                j--;
            }
        }
        return arr;
    }
}
